<!--
 * @Author: daidai
 * @Date: 2022-02-28 16:29:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-09-27 15:05:45
 * @FilePath: \web-pc\src\pages\big-screen\components\echart\index.vue
-->
<template>
  <div :id="id" ref="chart" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';

function debounce(fn, delay) {
  let timer = null;
  return function (...args) {
    timer = setTimeout(
      () => {
        typeof fn === "function" && fn.apply(null, args);
        clearTimeout(timer);
      },
      delay > 0 ? delay : 100
    );
  };
}

export default {
  name: 'echart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    options: {
      type: Object,
      default: () => ({})
    },
    mapJson: {
      type: Object,
      default: null,
    },
    // 是否随页面大小变化重新渲染图标
    isResize: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    options: {
      handler(options) {
        // 设置true清空echart缓存
        this.chart.setOption(options, true)
      },
      deep: true
    }
  },
  mounted() {
    this.initChart();
    if (this.isResize) {
      this.onResize = debounce(() => {
        this.resize();
      }, 500);
      this.$nextTick(() => {
        this.resize();
        this.addListener();
      });
    }
  },
  beforeDestroy() {
    this.chart.dispose();
    this.chart = null;
    if (this.isResize) {
      this.clearListener();
    }
  },
  methods: {
    initChart() {
      if (this.mapJson) {
        echarts.registerMap("bd", this.mapJson);
        this.chart = echarts.init(this.$el);
      } else {
        // 初始化echart
        this.chart = echarts.init(this.$el)
        this.chart.setOption(this.options, true)
      }
    },
    addListener() {
      window.addEventListener("resize", this.onResize);
    },
    clearListener() {
      window.removeEventListener("resize", this.onResize);
    },
    resize() {
      if (!this.chart) {
        return;
      }
      this.chart.resize({
        animation: {
          duration: 500,
        },
      });
    },
    saveAsImage(height = 800) {
      if (!this.chart) {
        this.$message.error('图表未初始化');
        return;
      }

      // 保存原始尺寸
      const originalWidth = this.$refs.chart.style.width;
      const originalHeight = this.$refs.chart.style.height;

      // 临时调整图表尺寸
      this.$refs.chart.style.width = originalWidth;
      this.$refs.chart.style.height = `${height}px`;
      this.chart.resize();

      // 获取图表图片的base64数据
      const imgData = this.chart.getDataURL({
        type: 'png',
        pixelRatio: 2, // 提高图片质量
        // backgroundColor: '#fff' // 背景色
      });

      // 恢复原始尺寸
      this.$refs.chart.style.width = originalWidth;
      this.$refs.chart.style.height = originalHeight;
      this.chart.resize();



      // 创建下载链接
      const link = document.createElement('a');
      link.href = imgData;
      link.download = 'echarts-chart.png'; // 下载文件名

      // 触发下载
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }
}
</script>

<style></style>
